package com.cisetech.web.bobocai.qiantai;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cisetech.service.CusTomerService;
import com.cisetech.service.LoginService;
import com.cisetech.util.Global;
import com.cisetech.util.email.EmailUtils;
import com.cisetech.vo.bobocai.LCustomer;


@Controller
@RequestMapping(value="qt/login")
public class QTLoginController {

	@Autowired
	LoginService loginServiceImpl;
	@Autowired
	CusTomerService cusTomerService;
	
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(HttpServletResponse response,HttpServletRequest request,Model model){
		String url = request.getHeader("Referer");  /*request.getServletPath()      //请求页面或其他地址
		 + "?" + (request.getQueryString()); //参数
*/		model.addAttribute("url", url);
		return "qiantai/login";
	}
	@ResponseBody
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public Map<String,String> loginMain(LCustomer customer,HttpServletResponse response,HttpServletRequest request){
		System.out.println("getEmail=="+customer.getEmail());
		Map<String,String> map = new HashMap<String,String>();
		String stats = "0";
		if(customer==null || customer.getEmail()==null || "".equals(customer.getEmail())){
			stats = "1";
			map.put("stats", stats);
			return map;
		}
		if(customer.getPassword()==null || "".equals(customer.getPassword())){
			stats = "2";
			map.put("stats", stats);
			return map;
		}
		stats = loginServiceImpl.qtLogin(customer,request);
		map.put("stats", stats);
		map.put("url", customer.getUrl());
		return map;
	}
	//判断是否登录会员0登录，1没登了
	@ResponseBody
	@RequestMapping(value="/isLogin",method=RequestMethod.POST)
	public Map<String,String> isLogin(HttpServletResponse response,HttpServletRequest request){
		Map<String,String> map = new HashMap<String,String>();
		String stats = "0";
		LCustomer customer = (LCustomer)request.getSession().getAttribute("LCustomer");
		if(customer!=null){
			stats = "0";
			map.put("name", customer.getName());
		}else{
			stats = "1";
		}
		map.put("stats", stats);
		return map;
	}
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String register(HttpServletResponse response,HttpServletRequest request){
		return "qiantai/register";
	}
	@ResponseBody
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public Map<String,String> registerMain(LCustomer customer,HttpServletResponse response,HttpServletRequest request){
		System.out.println("getEmail=="+customer.getEmail());
		Map<String,String> map = new HashMap<String,String>();
		map = loginServiceImpl.register(customer);
		String stats = map.get("stats");
		if("0".equals(stats)){
//			int i = cusTomerService.insert(customer);
//			if(i==1){
			try {
				EmailUtils.sendAccountActivateEmail(customer.getEmail(), "http://"+Global.getConfig("returnURL")+"/backReturn/register?name="+customer.getEmail()+"&d="+new Date().getTime());
			} catch (Exception e) {
				map.put("stats", "1");
				map.put("msg", "发送邮件失败，请查看你的邮箱格式或联系工作人员");
				return map;
			}
				
				map.put("msg", "恭喜你注册成功，请在5分钟之内前去邮箱验证，超时需要重新注册");
				//			}else{
//				stats = "1";
//				map.put("stats", stats);
//				map.put("msg", "注册失败请联系管理员");
//			}
		}
		return map;
	}
}
